package en.kaist.cs408.acav;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Message;
import en.kaist.cs408.acav.CreateClass.NetworkHandler;

public class WavNetworkThread extends Thread {
	Socket sc;
	String serverIP;
	int port;
	String courseID;
	String studentID;
	String voiceFileName;
	NetworkHandler wavHandler;
	Message msg;
	
	public WavNetworkThread(String serverIP, int port, String courseID, String studentID, String voiceFileName, NetworkHandler wavHandler){
		this.serverIP = serverIP;
		this.port = port;
		sc = null;
		
		this.courseID = courseID;
		this.studentID = studentID;
		this.voiceFileName = voiceFileName;
		
		this.wavHandler = wavHandler;
	}
	public void run(){
		try {
			sc = new Socket(serverIP, port);
			msg = wavHandler.obtainMessage();
			
			// InputStream and OutputStream
			InputStream is = sc.getInputStream();
			OutputStream os = sc.getOutputStream();

			// DataInputStrema and DataOutputStream
			DataOutputStream dos = new DataOutputStream(os);
			DataInputStream dis = new DataInputStream(is);

			// BufferedWriter and BufferedReader
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			
			dos.writeInt(1200);
			dos.flush();
			
			//bw.write(courseID + "_" + studentID + ".jpg\n");
			//bw.flush();
			
			//WavFileSender fs = new WavFileSender(sc, voiceFileName);
			WavFileSender fs = new WavFileSender(sc, voiceFileName, courseID, studentID, Thread.currentThread());
			fs.start();
			
			/*
			synchronized(Thread.currentThread()){
				try {
					Thread.currentThread().wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}*/
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.out.println("Thread woke up!!!!");
			
			dos.writeUTF(courseID);
			dos.flush();
			
			dos.writeUTF(studentID);
			dos.flush();
			
			
			int result = dis.readInt();
			System.out.println("Result: " + result);
			
			if (result == 0) {
				// Sucksex
			} else if (result == -10) {
				// Failed
			} else if (result == -100) {
				// SQLException
			} else if (result == -1000){
				// First time (registration)
			} else {
				// No course ID or No student ID in the course
			}
			
			msg.what = result;
			wavHandler.sendMessage(msg);
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
